package com.ruoyi.food.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.environment.domain.OpmEnvRule;
import com.ruoyi.food.domain.OpmFoodSafeCert;
import com.ruoyi.food.mapper.OpmFoodSafeCertMapper;
import com.ruoyi.food.service.IOpmFoodSafeCertService;
import com.ruoyi.system.api.LinkFileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 食品质量安全认证Service业务层处理
 *
 * @author cjie
 * @date 2021-07-24
 */
@Service
public class OpmFoodSafeCertServiceImpl extends LinkFileServiceImpl implements IOpmFoodSafeCertService {
    @Autowired
    private OpmFoodSafeCertMapper opmFoodSafeCertMapper;

    /**
     * 查询食品质量安全认证
     *
     * @param id 食品质量安全认证ID
     * @return 食品质量安全认证
     */
    @Override
    public OpmFoodSafeCert selectOpmFoodSafeCertById(Long id) {
        OpmFoodSafeCert result = opmFoodSafeCertMapper.selectOpmFoodSafeCertById(id);
        result.setFileList(getFileList(id.toString()));
        return result;
    }

    /**
     * 查询食品质量安全认证列表
     *
     * @param opmFoodSafeCert 食品质量安全认证
     * @return 食品质量安全认证
     */
    @Override
    public List<OpmFoodSafeCert> selectOpmFoodSafeCertList(OpmFoodSafeCert opmFoodSafeCert) {
        return opmFoodSafeCertMapper.selectOpmFoodSafeCertList(opmFoodSafeCert);
    }

    /**
     * 新增食品质量安全认证
     *
     * @param opmFoodSafeCert 食品质量安全认证
     * @return 结果
     */
    @Override
    public int insertOpmFoodSafeCert(OpmFoodSafeCert opmFoodSafeCert) {
        opmFoodSafeCert.setId(IdUtil.getSnowflake(0, 0).nextId());
        opmFoodSafeCert.setCreateTime(DateUtils.getNowDate());
        opmFoodSafeCert.setCreateBy(SecurityUtils.getUsername());
        linkFile(opmFoodSafeCert.getFileIdList(), opmFoodSafeCert.getId().toString());
        return opmFoodSafeCertMapper.insertOpmFoodSafeCert(opmFoodSafeCert);
    }

    /**
     * 修改食品质量安全认证
     *
     * @param opmFoodSafeCert 食品质量安全认证
     * @return 结果
     */
    @Override
    public int updateOpmFoodSafeCert(OpmFoodSafeCert opmFoodSafeCert) {
        opmFoodSafeCert.setUpdateTime(DateUtils.getNowDate());
        opmFoodSafeCert.setUpdateBy(SecurityUtils.getUsername());
        linkFile(opmFoodSafeCert.getFileIdList(), opmFoodSafeCert.getId().toString());
        return opmFoodSafeCertMapper.updateOpmFoodSafeCert(opmFoodSafeCert);
    }

    /**
     * 批量删除食品质量安全认证
     *
     * @param ids 需要删除的食品质量安全认证ID
     * @return 结果
     */
    @Override
    public int deleteOpmFoodSafeCertByIds(Long[] ids) {
        String[] res = new String[ids.length];
        for(int i=0;i<ids.length;i++){
            res[i] = String.valueOf(ids[i]);
        }
        deleteLinkFile(res);
        return opmFoodSafeCertMapper.deleteOpmFoodSafeCertByIds(ids);
    }

    /**
     * 删除食品质量安全认证信息
     *
     * @param id 食品质量安全认证ID
     * @return 结果
     */
    @Override
    public int deleteOpmFoodSafeCertById(Long id) {
        return opmFoodSafeCertMapper.deleteOpmFoodSafeCertById(id);
    }
}
